//
//  TWFXSingleClass.m
//  TWFXWeather
//
//  Created by Lion User on 13-9-30.
//  Copyright (c) 2013年 Lion User. All rights reserved.
//

#import "TWFXSingleClass.h"

static TWFXSingleClass* sharedSingleton = nil;

//static NSString *Key1_AQI = @"0f5875847fc46d0f26cca22760153c8c";
static NSString *Key2_AQI =@"1aa80560baa01b3ce14b4556719953b2";
static NSString *Key3_PMin = @"SUBuQK6GtSPxUWZrxSay";

static NSString *UserDefaultKey_currentCity = @"UserDefaultKey_currentCity";
static NSString *UserDefaultKey_cityDataList = @"UserDefaultKey_cityDataList";
static NSString *UserDefaultKey_indexOfCurrentCity = @"UserDefaultKey_indexOfCurrentCity";

@implementation TWFXSingleClass


+(TWFXSingleClass *)sharedTWFXSingleClass
{
    
    if (!sharedSingleton) {
        
       sharedSingleton = [[self alloc] init];
        
    }
    
    return sharedSingleton;
    
}


-(id) init
{
    if (self = [super init]) {
        
        _notificationName_currentCityIsChangedToTrend = @"_notificationName_currentCityIsChangedToTrend";
        _notificationName_currentCityIsChangedToRealTime = @"_notificationName_currentCityIsChangedToRealTime";
        _notificationName_currentCityIsReflesh = @"_notificationName_currentCityIsReflesh";
        _notificationName_CityDataListIsChanged = @"_notificationName_CityDataListIsChanged";
        
        
        self.isCityListInfosChangedToRealTime = NO;
        self.isCityListInfosChangedToTrend = NO;
        
        self.isCurrentCityChangedToRealTime = NO;
        self.isCurrentCityChangedToTrend = NO;
        
        self.isAddCity = NO;
                
        self.AQI_Key = Key3_PMin;
    }
    return self;
}


-(void)dealloc
{
    self.currentCityInfo = nil;
    self.cityDataList = nil;
    self.AQI_Key = nil;
    [super dealloc];
}

-(void)changeKey
{
    self.AQI_Key = Key2_AQI;
}


#pragma mark- 构造URL

-(NSURL*) URL_AQI:(NSString *)countName
{
    NSString *str = [NSString stringWithFormat:@"http://www.pm25.in/api/querys/only_aqi.json?city=%@&token=%@",countName,self.AQI_Key];
    
    NSString* encodedString = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    NSURL *url = [NSURL URLWithString:encodedString];
    return url;
}


-(NSURL*) URL_futureWeather:(NSString *)countID
{

    NSString *str = [NSString stringWithFormat:@"http://m.weather.com.cn/atad/%@.html",countID];
   
   //  NSString *str = [NSString stringWithFormat:@"http://wthrcdn.etouch.cn/weather_mini?citykey=%@",countID];
    
    NSString* encodedString = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    NSURL *url = [NSURL URLWithString:encodedString];
    return url;
}


-(NSURL*) URL_realTImeWeather:(NSString *)countID
{
    NSString *str = [NSString stringWithFormat:@"http://www.weather.com.cn/data/sk/%@.html",countID];
    
    NSString* encodedString = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    NSURL *url = [NSURL URLWithString:encodedString];

    return url;
}



#pragma mark- userDefault 读写 数据

-(void) saveIndexOfCurrentCityToUserDefault
{
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    
    //编码
    NSData *indexOfCurrentCity = [NSKeyedArchiver archivedDataWithRootObject:[NSString stringWithFormat:@"%d",self.indexOfCurrentCity]];
    
    //保存
    [userDefault setObject:indexOfCurrentCity forKey:UserDefaultKey_indexOfCurrentCity];
    
    //立刻保存
    [[NSUserDefaults standardUserDefaults] synchronize];
}


-(void) saveCurrentCityToUserDefault
{
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];

    //编码
    NSData *currentCity = [NSKeyedArchiver archivedDataWithRootObject:self.currentCityInfo];
    
    //保存
    [userDefault setObject:currentCity forKey:UserDefaultKey_currentCity];
    
    //立刻保存
    [[NSUserDefaults standardUserDefaults] synchronize];
}


-(void) saveCityListToUserDefault
{
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    
    //编码
    NSData *cityListData = [NSKeyedArchiver archivedDataWithRootObject:self.cityDataList];
    
    //保存
    [userDefault setObject:cityListData forKey:UserDefaultKey_cityDataList];
    
    //立刻保存
    [[NSUserDefaults standardUserDefaults] synchronize];
}


-(void) readIndexOfCurrentCityFromUserDefault
{
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    
    //读取
    NSData *indexOfCurrentCity = [userDefault objectForKey:UserDefaultKey_indexOfCurrentCity];
    
    //解码
    NSString *tempStr = [NSKeyedUnarchiver unarchiveObjectWithData:indexOfCurrentCity];
    
    self.indexOfCurrentCity = [tempStr intValue];

    
}


-(void) readCurrentCityFromUserDefault
{
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    
    //读取
    NSData *currentCity = [userDefault objectForKey:UserDefaultKey_currentCity];
    
    //解码
    self.currentCityInfo = [NSKeyedUnarchiver unarchiveObjectWithData:currentCity] ;

}


-(void) readCityListFromUserDefault
{
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    
    //读取
    NSData *cityDataList = [userDefault objectForKey:UserDefaultKey_cityDataList];
    
    //解码
    self.cityDataList = [NSKeyedUnarchiver unarchiveObjectWithData:cityDataList] ;
}




//当前城市的天气信息有更新,同步更新到城市列表里
-(void) CurrentCitySynToCityList
{
    for (int i = 0; i < self.cityDataList.count; i++) {
        
        TWFXStruct_AllWeatherInfo* itemCity = self.cityDataList[i];
        if ([self.currentCityInfo.countID isEqualToString: itemCity.countID]) {
            
            [self.cityDataList replaceObjectAtIndex:i withObject:self.currentCityInfo];
        }
    }
}


//在城市列表里删除的城市如果是当前城市,就把当前城市更换到城市列表里的第一个
-(void) deletedCityIsCurrentCity : (int) index
{
    TWFXStruct_AllWeatherInfo* itemCity = self.cityDataList[index];
    if ([self.currentCityInfo.countID isEqualToString:itemCity.countID]) {
        
        self.currentCityInfo = self.cityDataList[0];
    }
}




//程序刚开始运行时,从userDefault里读取数据初始化
-(void)initAPPData
{
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];


    BOOL isExist_cityDataList = [userDefault.dictionaryRepresentation.allKeys containsObject:UserDefaultKey_cityDataList];
    BOOL isExits_indexOfCurrentCity = [userDefault.dictionaryRepresentation.allKeys containsObject:UserDefaultKey_indexOfCurrentCity];
    
    //如果citylist存在
    if (isExist_cityDataList)
    {
        [self readCityListFromUserDefault];
                
        //如果index存在
        if (isExits_indexOfCurrentCity)
        {
            [self readIndexOfCurrentCityFromUserDefault];
            if (self.indexOfCurrentCity >= self.cityDataList.count)
            {
                self.indexOfCurrentCity = self.cityDataList.count - 1;
            }
        }
        //如果index不存在
        else
        {
            self.indexOfCurrentCity = 0;
            
            //保存到userdefault
            [self saveCurrentCityToUserDefault];

        }
    }
    //如果citylist不存在
    else {
        
        TWFXStruct_AllWeatherInfo* cityTempWeather = [[[TWFXStruct_AllWeatherInfo alloc] init] autorelease];
        cityTempWeather.countName =  @"广州";
        cityTempWeather.countID = @"101280101";
        cityTempWeather.countPY = @"guangzhou";
        cityTempWeather.cityName = @"广州";
        cityTempWeather.cityPY = @"guangzhou";
        cityTempWeather.proName = @"广东";

        
        //构造cityList
        self.cityDataList = [[[NSMutableArray alloc] initWithObjects:cityTempWeather, nil] autorelease];
        self.indexOfCurrentCity = 0;
        
        //保存到userdefault
        [self saveCityListToUserDefault];
        [self saveIndexOfCurrentCityToUserDefault];

    }

}



@end





